package com.liang.lollipop.lreader.util;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Lollipop on 2017/09/06.
 * 数据库管理工具
 */
public class DBUtil extends SQLiteOpenHelper {

    private static final String DB_NAME = "LReader.db";
    private static final int DB_VERSION = 2;

    static final String MODEL_TABLE = "MODEL_TABLE";
    static final String MODEL_ID = "MODEL_ID";
    static final String MODEL_NAME = "MODEL_NAME";
    static final String MODEL_VALUE = "MODEL_VALUE";
    static final String MODEL_PROJECT = "MODEL_PROJECT";
    static final String MODEL_SORT = "MODEL_SORT";

    static final String HISTORY_TABLE = "HISTORY_TABLE";
    static final String HISTORY_ID = "HISTORY_ID";
    static final String HISTORY_TIME = "HISTORY_TIME";
    static final String HISTORY_NAME = "HISTORY_NAME";
    static final String HISTORY_PKG = "HISTORY_PKG";

    private static final String CREATE_MODEL_TABLE = "create table "+MODEL_TABLE+"(" +
            MODEL_ID+" integer PRIMARY KEY, " +
            MODEL_NAME+" varchar, " +
            MODEL_VALUE+" integer default 0, " +
            MODEL_PROJECT+" varchar, " +
            MODEL_SORT+" integer default 10 "+
            ");";

    private static final String CREATE_HISTORY_TABLE = "create table "+HISTORY_TABLE+"(" +
            HISTORY_ID+" integer PRIMARY KEY, " +
            HISTORY_TIME+" varchar, " +
            HISTORY_NAME+" varchar, " +
            HISTORY_PKG+" varchar  " +
            ");";

    public DBUtil(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_MODEL_TABLE);
        db.execSQL(CREATE_HISTORY_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(oldVersion==1&&newVersion==2){
            db.execSQL(CREATE_HISTORY_TABLE);
        }
    }
}

